from appserver import db_store


"""
设备与传感器的中间表,第三张表,存储设备和传感器的外键id
"""

class DeviceWithSensor(db_store.Model):
    __tablename__ = "device_with_sensor"
    __main__ = "main"

    device_id = db_store.Column(db_store.Integer, db_store.ForeignKey('devices.id'), primary_key=True, nullable=False, comment=u"设备id")
    sensor_id = db_store.Column(db_store.Integer, db_store.ForeignKey('sensors.id'), primary_key=True, nullable=False, comment=u"传感器id")

    def __repr__(self):
        return "<DeviceWithSensor %d+%d>" %(self.device_id,self.sensor_id)
    
    def save(self):
        try:
            db_store.session.add(self)
            db_store.session.commit()
            return True
        except Exception as e:
            db_store.session.rollback()
            print(e)
            return False
    
    def delete(self):
        try:
            db_store.session.delete(self)
            db_store.session.commit()
            return True
        except Exception as e:
            print(e)
            db_store.session.rollback()
            return False
